﻿using System;
using TeamCommunitySolution.Domain.DataLayer.Venues;
using NHibernate;
using log4net;
using TeamCommunitySolution.Services.Factories;
using TeamCommunitySolution.Services.Repositories;

namespace TeamCommunitySolution.Services.Repositories.Impl
{
    public class VenueRepository : IVenueRepository
    {

        /// <summary>
        /// Logger for class
        /// </summary>
        private static readonly ILog _log = LogManager.GetLogger(typeof(VenueRepository));

        /// <summary>
        /// The repository instance.
        /// </summary>
        private static IVenueRepository _repo = null;

        private VenueRepository() { }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static IVenueRepository Instance
        {
            get
            {
                if (_repo == null)
                {
                    _repo = new VenueRepository();
                }

                return _repo;
            }
        }

        #region IVenueRepository Members

        /// <summary>
        /// Saves the venue.
        /// </summary>
        /// <param name="venue">The venue.</param>
        public void SaveVenue(Venue venue)
        {

            using (ISession session = NHSessionFactory.Factory.OpenSession())
            {
                ITransaction scope = session.BeginTransaction();
                try
                {

                    session.SaveOrUpdate(venue);

                    scope.Commit();
                }
                catch (Exception ex)
                {
                    scope.Rollback();
                    string message = "Unable to save object [Venue] in the datastore";
                    this.HandleException(message, ex);
                }
            }
        }

        /// <summary>
        /// Gets the venue.
        /// </summary>
        /// <param name="id">The id of the venue.</param>
        /// <returns>Venue instance.</returns>
        public Venue GetVenue(string id)
        {
            using (ISession session = NHSessionFactory.Factory.OpenSession())
            {
                try
                {
                    return session.Get<Venue>(id);
                }
                catch (Exception ex)
                {
                    string message = "Unable to find object [Venue] in the datastore";
                    this.HandleException(message, ex);
                }
            }

            return null;
        }

        /// <summary>
        /// Removes the venue.
        /// </summary>
        /// <param name="venueId">The venue id.</param>
        public void RemoveVenue(string venueId)
        {
            using (ISession session = NHSessionFactory.Factory.OpenSession())
            {
                ITransaction scope = session.BeginTransaction();
                try
                {
                    session.Delete(this.GetVenue(venueId));

                    scope.Commit();
                }
                catch (Exception ex)
                {
                    scope.Rollback();
                    string message = "Unable to delete object [Venue] from the datastore";
                    this.HandleException(message, ex);
                }
            }
        }

        #endregion

        /// <summary>
        /// Handles the exception.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="ex">The exception.</param>
        private void HandleException(string message, Exception ex)
        {
            _log.Error(message, ex);
            throw new VenueRepositoryException(message, ex);
        }
    }
}
